<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input v-model="queryParams.orderNumber" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="交易日期" prop="nowDate">
        <el-date-picker
          v-model="queryParams.nowDate"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="商家微信名" prop="merchantWechat">
        <el-input v-model="queryParams.merchantWechat" placeholder="请输入商家微信名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="微信号" prop="wechatID">
        <el-input v-model="queryParams.wechatID" placeholder="请输入微信号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入创建人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="返款进度" prop="whetherRepay">
        <el-select v-model="queryParams.whetherRepay" placeholder="请选择返款进度" clearable>
          <el-option v-for="dict in dict.type.bing_fkzt" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:course:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:course:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:course:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="multiple" @click="handleUpdateRate"
          v-hasPermi="['system:course:editRate']">修改汇率</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:course:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" class="table" :data="courseList" @selection-change="handleSelectionChange" :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderNumber" width="240"/>
      <el-table-column label="创建人" align="center" prop="createBy" width="240"/>
      <el-table-column label="交易日期" align="center" prop="nowDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nowDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商家微信名" align="center" prop="merchantWechat" width="180"/>
      <el-table-column label="微信号" align="center" prop="wechatID" width="180"/>
      <el-table-column label="产品" align="center" prop="producteName" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="订单类型要求" align="center" prop="orderTyp">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bing_ddlx" :value="scope.row.orderTyp ? scope.row.orderTyp.split(',') : []" />
        </template>
      </el-table-column>
      <el-table-column label="联系账号" align="center" prop="contactAccountNumber" />
      <el-table-column label="下单账号" align="center" prop="issueOrderAccount" />
      <el-table-column label="收货人" align="center" prop="consignee" />
      <el-table-column label="返款进度" align="center" prop="whetherRepay">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bing_fkzt" :value="scope.row.whetherRepay"/>
        </template>
      </el-table-column>
      <el-table-column label="返款方式" align="center" prop="repayTyp">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bing_fkfs" :value="scope.row.repayTyp" />
        </template>
      </el-table-column>

      <el-table-column label="成汇" align="center" prop="exchangeRate" />
      <el-table-column label="成本" align="center" prop="cost" />
      <el-table-column label="返汇" align="center" prop="returnRate" />
      <el-table-column label="预估反款" align="center" prop="estimatedCounterpayment" />
      <el-table-column label="预估利润" align="center" prop="estimatedProfit" />
      <el-table-column label="真实利润" align="center" prop="realProfit" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:course:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:course:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <div style="max-height:500px;overflow-y: auto;">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单编号" />
        </el-form-item>
        <!-- <el-form-item label="交易日期" prop="nowDate">
          <el-date-picker clearable v-model="form.nowDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择交易日期">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="商家微信名" prop="merchantWechat">
          <el-input v-model="form.merchantWechat" placeholder="请输入商家微信名" />
        </el-form-item>
        <el-form-item label="微信号" prop="wechatID">
          <el-input v-model="form.wechatID" placeholder="请输入微信号" />
        </el-form-item>
        <el-form-item label="产品" prop="producteName">
          <el-input v-model="form.producteName" placeholder="请输入产品" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="订单类型要求" prop="orderTyp">
          <el-checkbox-group v-model="form.orderTyp">
            <el-checkbox v-for="dict in dict.type.bing_ddlx" :key="dict.value" :label="dict.value">
              {{ dict.label }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="联系账号" prop="contactAccountNumber">
          <el-input v-model="form.contactAccountNumber" placeholder="请输入联系账号" />
        </el-form-item>
        <el-form-item label="下单账号" prop="issueOrderAccount">
          <el-input v-model="form.issueOrderAccount" placeholder="请输入下单账号" />
        </el-form-item>
        <el-form-item label="收货人" prop="consignee">
          <el-input v-model="form.consignee" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="返款进度" prop="whetherRepay">
          <el-select v-model="form.whetherRepay" placeholder="请选择返款进度" clearable>
            <el-option v-for="dict in dict.type.bing_fkzt" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="返款方式" prop="repayTyp">
          <el-select v-model="form.repayTyp" placeholder="请选择返款方式" clearable>
            <el-option v-for="dict in dict.type.bing_fkfs" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="成汇" prop="exchangeRate">
          <el-input v-model="form.exchangeRate" placeholder="请输入成汇" />
        </el-form-item>
        <el-form-item label="成本" prop="cost">
          <el-input v-model="form.cost" placeholder="请输入成本" />
        </el-form-item>
        <el-form-item label="返汇" prop="returnRate">
          <el-input v-model="form.returnRate" placeholder="请输入返汇" />
        </el-form-item>
        <el-form-item label="预估反款" prop="estimatedCounterpayment">
          <el-input v-model="form.estimatedCounterpayment" placeholder="请输入预估反款" />
        </el-form-item>
        <el-form-item label="预估利润" prop="estimatedProfit">
          <el-input v-model="form.estimatedProfit" placeholder="请输入预估利润" />
        </el-form-item>
        <!-- <el-form-item label="真实利润" prop="realProfit">
          <el-input v-model="form.realProfit" placeholder="请输入真实利润" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openRate" width="1000px" append-to-body>
      <div style="max-height:500px;overflow-y: auto;">
        <el-form ref="formRate" :model="formRate" :rulesRate="rulesRate" label-width="80px">
          <el-form-item label="成汇" prop="exchangeRate">
            <el-input v-model="formRate.exchangeRate" placeholder="请输入成汇" />
          </el-form-item>
          <el-form-item label="返汇" prop="returnRate">
            <el-input v-model="formRate.returnRate" placeholder="请输入返汇" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRateForm">确 定</el-button>
        <el-button @click="cancelRate">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCourse, getCourse, delCourse, addCourse, updateCourse, updateRateCourse} from "@/api/system/course";

export default {
  name: "Course",
  dicts: ['bing_fkzt', 'bing_ddlx', 'bing_fkfs'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示修改利率弹出层
      openRate: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        remarks: null,
        nowDate: null,
        merchantWechat: null,
        wechatID: null,
        producteName: null,
        price: null,
        orderTyp: null,
        ids: null,
        contactAccountNumber: null,
        issueOrderAccount: null,
        consignee: null,
        whetherRepay: null,
        repayTyp: null,
        exchangeRate: null,
        cost: null,
        returnRate: null,
        estimatedCounterpayment: null,
        estimatedProfit: null,
        realProfit: null,
        nowDateStartTime: null,
        nowDateEndTime: null
      },
      // 表单参数
      form: {},
      // 利率表单参数
      formRate: {},
      // 表单校验
      rules: {
        merchantWechat: [
          { required: true, message: "商家微信名不能为空", trigger: "blur" }
        ],
        wechatID: [
          { required: true, message: "微信号不能为空", trigger: "blur" }
        ],
        producteName: [
          { required: true, message: "产品不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        orderTyp: [
          { required: true, message: "订单类型要求不能为空", trigger: "blur" }
        ],
        contactAccountNumber: [
          { required: true, message: "联系账号不能为空", trigger: "blur" }
        ],
        // issueOrderAccount: [
        //   { required: true, message: "下单账号不能为空", trigger: "blur" }
        // ],
        // consignee: [
        //   { required: true, message: "收货人不能为空", trigger: "blur" }
        // ],
        whetherRepay: [
          { required: true, message: "返款进度不能为空", trigger: "change" }
        ],
      },
      // 利率表单校验
      rulesRate: {
        exchangeRate : [
          { required: true, message: "成汇不能为空", trigger: "blur" }
        ],
        returnRate: [
          { required: true, message: "返汇不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // getWhetherRepayStyle(val) {
    //   if(val == "等待返款") return { color: "#ff0000" };
    //     return { color: "red" };
    // },
    tableRowClassName({row, rowIndex}) {
        return row.whetherRepay;
      },
    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      if(Array.isArray(this.queryParams.nowDate) && this.queryParams.nowDate.length === 2){
        this.queryParams.nowDateStartTime = this.queryParams.nowDate[0];
        this.queryParams.nowDateEndTime = this.queryParams.nowDate[1];
        delete this.queryParams.nowDate;
      }
      listCourse(this.queryParams).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // getRowBackgroundColor(item) {
    //   // 根据列表中某一个字段的字段值来控制表格这行的背景色，例如：如果 field1 等于 '值1'，则返回 'red'，否则返回 'white'
    //   if (item.whetherRepay === '等待返款') {
    //     return 'red'
    //   } else {
    //     return 'white'
    //   }
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelRate() {
      this.openRate = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        remarks: null,
        nowDate: null,
        merchantWechat: null,
        wechatID: null,
        producteName: null,
        price: null,
        orderTyp: [],
        contactAccountNumber: null,
        issueOrderAccount: null,
        consignee: null,
        whetherRepay: null,
        repayTyp: null,
        exchangeRate: null,
        cost: null,
        returnRate: null,
        estimatedCounterpayment: null,
        estimatedProfit: null,
        realProfit: null
      };
      this.resetForm("form");
    },
    // 表单重置
    reset() {
      this.formRate = {
        exchangeRate : null,
        returnRate: null
      };
      this.resetForm("formRate");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCourse(id).then(response => {
        this.form = response.data;
        this.form.orderTyp = this.form.orderTyp.split(",");
        this.open = true;
        this.title = "修改订单信息";
      });
    },

    /** 修改汇率按钮操作 */
    handleUpdateRate(row) {
      this.reset();
      const id = row.id || this.ids
      getCourse(id).then(response => {
        this.form = response.data;
        this.form.orderTyp = this.form.orderTyp.split(",");
        this.openRate = true;
        this.title = "修改订单汇率信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.orderTyp = this.form.orderTyp.join(",");
          if (this.form.id != null) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 利率修改提交按钮 */
    submitRateForm() {
      this.$refs["formRate"].validate(valid => {
        if (valid) {
          updateRateCourse({
            ...this.formRate,
            ids: this.ids
          }).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.openRate = false;
            this.getList();
          });
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单信息编号为"' + ids + '"的数据项？').then(function () {
        return delCourse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 修改利率按钮操作 */
    // handleUpdateRate(row) {
    //   this.$refs["form"].validate(valid => {
    //   const ids = row.id || this.ids;
    //   this.form.ids = this.form.orderTyp.join(",");
    //   this.$modal.confirm('是否确认修改利率订单信息编号为"' + ids + '"的数据项？').then(function () {
    //     return updateRate(this.form);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("修改成功");
    //   }).catch(() => { });

    // })
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/course/export', {
        ...this.queryParams
      }, `course_${new Date().getTime()}.xlsx`)
    },
    data() {
      return {
        value1: '',
        value2: ''
      };
    }
  }
};
</script>

<style scoped>
.el-table /deep/ .ddfk{
  background-color: #ffd585!important;
}
.el-table /deep/ .ytk{
  background-color: #d25d5d!important;
}
.el-table /deep/ .yfk{
  background-color: #63d15d!important;
}
.el-table /deep/ .ppfk{
  background-color: #8497ce!important;
}
</style>
